package com.meiwei.transaction.user.ext

import android.view.View
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.core.view.children

/**
 * @author WEN
 * @Description:
 * @date 2022-3-17 21:28
 */

/**
 * 用于LinearLayoutCompat 单选或者多选
 */
fun LinearLayoutCompat.initialSelect(initSelectIndex:Int = 0,isMultiple:Boolean = false,callBackClick:(view: View, result:Int) -> Unit){
    val onClickListener = View.OnClickListener {
        if(isMultiple){
            it.isSelected = !it.isSelected
        }else{
            val index = indexOfChild(it)
            for (i in 0 until childCount){
                val childView = getChildAt(i)
                childView.isSelected = index == i
            }
        }
    }
    if(initSelectIndex in 0 until childCount){
        getChildAt(initSelectIndex).isSelected = true
    }
    for (i in 0 until childCount){
        val childView = getChildAt(i)
        childView.setOnClickListener(onClickListener)
    }
}
